package com.zxp.web.utils;

import com.zxp.web.constants.Constants;
import com.zxp.web.sys_menu.entity.RouterVO;
import com.zxp.web.sys_menu.entity.SysMenu;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author MasterZ
 * @Date 2025/10/4 09:49
 */
public class SystemUtils {
    //树形工具
    public static List<SysMenu> toTree(List<SysMenu> menuList, Long pid) {
        List<SysMenu> list = new ArrayList<>();//存放组装的树
        Optional.ofNullable(menuList).orElse(new ArrayList<>()).stream()
                .filter(item -> item != null && item.getParentId().equals(pid))
                .forEach(item -> {
                    SysMenu menu = new SysMenu();
                    BeanUtils.copyProperties(item, menu);
                    menu.setLabel(item.getTitle());
                    menu.setValue(item.getMenuId());
                    //查找下级
                    menu.setChildren(toTree(menuList, item.getMenuId()));
                    list.add(menu);
                });
        return list;
    }

    //组装路由
    public static List<RouterVO> toTreeRouter(List<SysMenu> menuList, Long pid) {
        List<RouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getParentId().equals(pid))
                .forEach(item -> {
                    //组装路由
                    RouterVO routerVO = new RouterVO();
                    routerVO.setName(item.getName());
                    routerVO.setPath(item.getPath());
                    //设置children，递归
                    List<RouterVO> children = toTreeRouter(menuList, item.getMenuId());
                    routerVO.setChildren(children);
                    if (item.getParentId().equals(Constants.MENU_ROOT_ID)) { //如果上级是顶级菜（本身就是一级菜单）单那么他的component是Layout
                        routerVO.setComponent("Layout");
                        //判断该数据是目录还是菜单
                        if (item.getType().equals(Constants.MENU_TYPE_MENUITEM)) {//如果一级菜单有链接的比如首页这种，继续设置其他属性
                            routerVO.setRedirect(item.getPath());
                            //一级菜单有链接的只有一个孩子
                            List<RouterVO> listChild = new ArrayList<>();
                            RouterVO child = new RouterVO();
                            child.setName(item.getName());
                            child.setPath(item.getPath());
                            child.setComponent(item.getUrl());
                            child.setMeta(child.new Meta(
                                    item.getTitle(),
                                    item.getIcon(),
                                    item.getCode().split(",")
                            ));
                            listChild.add(child);
                            routerVO.setChildren(listChild);
                            routerVO.setPath(item.getPath() + "parent");
                            routerVO.setName(item.getName() + "parent");
                        }
                    } else {
                        routerVO.setComponent(item.getUrl());
                    }
                    routerVO.setMeta(routerVO.new Meta(
                            item.getTitle(),
                            item.getIcon(),
                            item.getCode().split(",")
                    ));
                    list.add(routerVO);
                });
        return list;
    }

    public static SysMenu getRoot() {
        SysMenu root = new SysMenu();
        root.setTitle("顶级菜单");
        root.setName("顶级菜单");
        root.setParentId(Constants.MENU_ROOT_PARENT_ID);
        root.setMenuId(Constants.MENU_ROOT_ID);
        root.setValue(Constants.MENU_ROOT_ID);
        return root;
    }
}